package com.vma.jifu.others.impl;

import com.jfinal.plugin.activerecord.Page;
import com.vma.jifu.bizModel.model.TgArticle;
import com.vma.jifu.bizModel.model.TgVedio;
import com.vma.jifu.others.TgAssistService;
import com.vma.tools.AppException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenzui on 2017/6/16.
 */
public class TgAssistServiceImpl implements TgAssistService {
    public List<TgArticle> getArticles() {
        List<TgArticle> articles = new ArrayList<TgArticle>();
        articles = TgArticle.dao.find("SELECT * FROM tg_article WHERE status=1 ORDER BY create_time DESC LIMIT 2    ");
        if(articles.size()==0){
            throw new AppException("-1","无图文消息");
        }
        return articles;
    }

    public List<TgVedio> getVedioes() {
        List<TgVedio> vedioes = new ArrayList<TgVedio>();
        vedioes = TgVedio.dao.find("SELECT * FROM tg_vedio WHERE status = 1 ORDER BY create_time DESC LIMIT 2");
        if(vedioes.size()==0){
            throw new AppException("-1","无图文消息");
        }
        return vedioes;
    }

    public Page<TgArticle> getArticleByPage(Integer cPage, Integer pSize) {
        Page<TgArticle> articlePage = TgArticle.dao.paginate(cPage,pSize,
                "SELECT *",
                "FROM tg_article WHERE status=1 ORDER BY create_time DESC");

        return articlePage;
    }

    public Page<TgVedio> getVedioByPage(Integer cPage, Integer pSize) {
        Page<TgVedio> vedioPage = TgVedio.dao.paginate(cPage,pSize,
                "SELECT *",
                "FROM tg_vedio WHERE status=1 ORDER BY create_time DESC");

        return vedioPage;
    }

    public TgArticle getArticleById(Integer id) {
        return TgArticle.dao.findById(id);
    }

    public TgVedio getVedioById(Integer id) {
        return TgVedio.dao.findById(id);
    }
}
